import cv2
import numpy as np

def phash(image, hash_size=8):
    """计算图像的感知哈希"""
    image = cv2.resize(image, (hash_size + 1, hash_size))
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    diff = gray[:, 1:] > gray[:, :-1]
    return sum(2 ** i for i, val in enumerate(diff.flatten()) if val)

def extract_video_fingerprint(video_path):
    """提取视频指纹（关键帧哈希组合）"""
    cap = cv2.VideoCapture(video_path)
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    key_frames = [0, frame_count // 2, frame_count - 1]
    
    hashes = []
    for i in range(frame_count):
        ret, frame = cap.read()
        if not ret or i not in key_frames:
            continue
        hashes.append(phash(frame))
    
    cap.release()
    return "-".join(map(str, hashes))